<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
namespace Shop\Api;
use Common\Api\Api;
use Shop\Model\GroupModel;

class GroupApi extends Api{


    /**
     * 查询,不分页
     */
    const QUERY_NO_PAGING = "Shop/Group/queryNoPaging";
    /**
     * 添加
     */
    const ADD = "Shop/Group/add";
    /**
     * 保存
     */
    const SAVE = "Shop/Group/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "Shop/Group/saveByID";

    /**
     * 删除
     */
    const DELETE = "Shop/Group/delete";

    /**
     * 查询
     */
    const QUERY = "Shop/Group/query";
    const QUERYLI = "Shop/Group/queryli";
    /**
     * 查询一条数据
     */
    const GET_INFO = "Shop/Group/getInfo";
    /**
     * 查询类目属性的值表
     */
    const QUERY_PROP_TABLE = "Shop/Group/queryPropTable";
    const WEI = "Shop/Group/wei";
    const QUERY_GROUPDETAIL = "Shop/Group/querygroupdetail";


    protected function _init(){
        $this->model = new GroupModel();
    }

    public function wei($map,$page = array('curpage'=>0,'size'=>5), $params = false){
        $query = $this -> model;
        $mapp='group_sum>=group_count';
        $list = $query->alias('gm')->join('itboye_wxuser as wu on wu.id=gm.uid','left')->where($map)->where($mapp)
            ->order('createtime desc')
            ->page($page['curpage'] . ',' . $page['size'])
            ->field('gm.*,wu.nickname')
            ->select();
        //var_dump($list);echo $query->getlastsql();exit;
        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }
        $count = $this -> model->alias('gm') ->  where($map)->where($mapp)-> count();
        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);
        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }
        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();
        return $this -> apiReturnSuc(array("show" => $show, "list" => $list));
    }

    public function queryli($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false) {
        $query = $this->model;

        $list = $query->alias('gm')
            ->join('itboye_product as p on p.id =gm.productid','left')
            ->where($map)
            ->order($order)->field('gm.*,p.name')
            -> page($page['curpage'] . ',' . $page['size'])
            ->select();


//        if ($list === false) {
//            $error = $this -> model -> getDbError();
//            return $this -> apiReturnErr($error);
//        }

        $count = $this -> model ->alias('gm')->join('itboye_product as p on p.id =gm.productid','left')
            ->where($map)
            ->order($order)->field('gm.*,p.name')
            -> page($page['curpage'] . ',' . $page['size']) -> count();
        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();

        return $this -> apiReturnSuc(array("show" => $show, "list" => $list));
    }



}


